package com.feeyo.groundservice.vehiclemonitorserver;

import java.util.Date;

import com.feeyo.groundservice.base.rabbitmq.RMQ;
import com.feeyo.groundservice.base.rdb.PostgresDb;
import com.feeyo.groundservice.base.redis.Redis;
import com.feeyo.groundservice.base.util.GlobalConstant;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import redis.clients.jedis.ShardedJedis;


/**
 * 
 * @author liulei
 * <p>Description: <／p>
 * @date 2017年4月28日
 * @param type （限制区，超速）报警开始 报警结束 司机上车 司机下车 保障开始 保障结束 
 */
public class MonitorUtils {
	
    public static void log(String vehicleId, int eventType, String airport, JsonObject describe,String limitArea,double speed){
        int time =  Math.round(new Date().getTime() / 1000);
        //事件写入MQ
        RMQ.getRMQInstance().getRMQClient().basicPublish(airport, airport+"...."+vehicleId+".EVENT", new JsonObject(){{
            put("body", new JsonObject(){{
                put("_trigger_source", new JsonObject(){{put("schema", "vehiclemonitor"); put("table", "vehicle"); put("pk", new JsonObject(){{put("id", vehicleId);}});}});
                put("type", eventType);
                put("description", describe);
                put("time", time);
            }}.toString());
        }}, r->{
            if (r.failed()){
                System.out.println(r.cause());
            }
        });
        //事件写入关系数据库postgreSql
        PostgresDb.postgreSQLClient.getConnection(getConn ->{
            if (getConn.failed()){
                System.out.println(getConn.cause());
            }
            if (getConn.succeeded()){
            	ShardedJedis jedis = Redis.pool.getResource();
        		String eventid = jedis.hget(GlobalConstant.VEHICLE_REALTIME_STATUS+vehicleId, "eventid");
        		
            	if ((eventType == VehicleStatus.OVERSPEED_AREA_IN.getKey())||(eventType == VehicleStatus.FORBID_AREA_IN.getKey())){
            		System.out.println(vehicleId + ":"+ eventType);
                    getConn.result().queryWithParams("insert into vehiclemonitor.vehicleevent (_trigger_source,type,description, intime,vehicleid,maxspeed,area,eventid) "
                    		+ " values (?::jsonb,?,?::jsonb,?,?::uuid,?,?,?)",
                    	new JsonArray(){{
                        add(new JsonObject(){{put("schema", "vehiclemonitor"); put("table", "vehicle"); put("pk", new JsonObject(){{put("id", vehicleId);}});}}.toString());
                        add(eventType);
                        add(describe.toString());
                        add(time);
                        add(vehicleId);
                        add(limitArea);
                        add(speed);
                        add(Long.parseLong(eventid));
                    	}},
                    	r->{
                    		getConn.result().close();
                    });
            	}else if ((eventType == VehicleStatus.OVERSPEED_AREA_OUT.getKey())||(eventType == VehicleStatus.FORBID_AREA_OUT.getKey())){
            		if (!(eventid == null)&&(!eventid.equals(""))){
	                    getConn.result().queryWithParams("update vehiclemonitor.vehicleevent  SET outtime=? WHERE eventid=?",
	                    	new JsonArray(){{
	                        add(time);
	                        add(Long.parseLong(eventid));
	                    	}},
	                    	r->{
	                    		getConn.result().close();
	                    });
            		}
            	}

            }
        });
    }

    public static double[] latlonToEPSG3857 (double lat, double lon){
        double x = lon * 20037508.34 / 180;
        double y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
        y = y * 20037508.34 / 180;
        return new double[]{y,x};
    }
}
